<template>
  <el-dialog title="续签合同"
             :visible.sync="dialogForm"
             width="1100px">
    <el-form :inline="true"
             ref="form"
             :model="form"
             :rules="rules"
             class="add-form"
             label-width="84px">
      <h3>基础信息</h3>
      <hr />
      <br />
      <el-row>
        <el-col :span="8">
          <el-form-item label="电子编号"
                        prop="eleCode">
            <el-input type="text"
                      v-model.trim="form.eleCode"
                      disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同号"
                        prop="code">
            <el-input type="text"
                      v-model.trim="form.code"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="账户名称"
                        prop="cargoOwnerAccountName">
            <el-select v-model="form.cargoOwnerAccountName"
                       placeholder="请选择"
                       @change="handleChange">
              <el-option v-for="(item,index) in accountList"
                         :key="index"
                         :label="item.name"
                         :value="item.name"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="客户名称"
                        prop="partyAName">
            <el-select v-model="form.partyAName"
                       placeholder="请选择"
                       disabled>
              <el-option v-for="(item,index) in accountList"
                         :key="item.customerId"
                         :label="item.customerName"
                         :value="item.customerId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="有效期起"
                        prop="startDate">
            <el-date-picker v-model="form.startDate"
                            :picker-options="startPickerOptions"
                            value-format="yyyy-MM-dd"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="有效期止"
                        prop="endDate">
            <el-date-picker v-model="form.endDate"
                            value-format="yyyy-MM-dd"
                            :picker-options="endPickerOptions"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同名称"
                        prop="name">
            <el-input type="text"
                      v-model.trim="form.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="签订时间"
                        prop="signDate">
            <el-date-picker v-model="form.signDate"
                            value-format="yyyy-MM-dd"
                            :picker-options="endPickerOptions"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="免保管费"
                        prop="ifFreeSafeguard">
            <el-select v-model="form.ifFreeSafeguard"
                       placeholder="请选择">
              <el-option v-for="[key, val] in _dictTrueOrFalse"
                         :key="key"
                         :label="val"
                         :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库结算"
                        prop="inSettlementMode">
            <el-select v-model="form.inSettlementMode"
                       placeholder="请选择">
              <el-option v-for="[key, val] in _dictInSettleStyle"
                         :key="key"
                         :label="val"
                         :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="出库结算"
                        prop="outSettlementMode">
            <el-select v-model="form.outSettlementMode"
                       placeholder="请选择">
              <el-option v-for="[key, val] in _dictOutSettleStyle"
                         :key="key"
                         :label="val"
                         :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属单位"
                        prop="orgId">
            <auto-search-org v-model="form.orgId"
                             @select="v=>{form.orgId=v;form.deptId='';form.deptName=''}"
                             @selectName="v=>form.orgName=v"
                             :disabled="true"
                             :name="form.orgName"></auto-search-org>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属仓库"
                        prop="warehouseName">
            <el-select v-model="form.warehouseName"
                       placeholder="请选择">
              <el-option v-for="item in warehouseList"
                         :key="item.id"
                         :label="item.name"
                         :value="item.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注"
                        prop="remark">
            <el-input type="textarea"
                      :rows="1"
                      style="width: 188px"
                      placeholder="请输入内容"
                      :maxLength="$maxLength"
                      v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <h3>合同附件</h3>
      <hr />
      <br />
      <el-row>
        <el-col :span="12">
          <el-form-item label="附件"
                        class="customFormWidth">
            <attachments v-model="form.attachments"></attachments>
            <download v-model="form.attachments"></download>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="3">
          <h3>计费规则设置 </h3>
        </el-col>
      </el-row>
      <hr />
      <br />
      <el-row>
        <el-col>
          <el-button type="info"
                     plain
                     @click="handleAccountRuleForm">添加计费规则</el-button>
        </el-col>
      </el-row>
      <el-row class="box"
              v-for="(item,index) in form.contractRules"
              :key="index">

        <el-row class="title">
          <el-col :span="22">
            <h3>{{item.feeRuleName}}</h3>
          </el-col>
          <el-col :span="2">
            <el-button @click="addAccountRule(item)">添加</el-button>
          </el-col>
        </el-row>
        <el-row class="block">
          <el-col :span="22">
            <span>有效期</span>
            <el-date-picker v-model="item.startDate"
                            :picker-options="startPickerOptions"
                            value-format="yyyy-MM-dd"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
            <span>——</span>
            <el-date-picker v-model="item.endDate"
                            value-format="yyyy-MM-dd"
                            :picker-options="endPickerOptions"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
          </el-col>
          <el-col :span="2">
            <el-button @click="removeAccountRule(item)">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="item.rates"
                  border>
          <el-table-column label="序号"
                           min-width="50">
            <template slot-scope="scope">
              {{ scope.$index+1 }}
            </template>
          </el-table-column>
          <el-table-column :key="item2.id"
                           :label="item2.name"
                           min-width="220"
                           v-for="(item2, index2) in item.tableList">
            <template slot-scope="scope">
              <el-col :span="12">
                <el-select v-model="item.rates[scope.$index].factor[index2].operator"
                           placeholder="请选择"
                           disabled
                           clearable>
                  <el-option v-for="[key, val] in _dictRuleOperator"
                             :key="key"
                             :label="val"
                             :value="key"></el-option>
                </el-select>
              </el-col>
              <el-col :span="12">
                <el-select v-model="item.rates[scope.$index].factor[index2].typeValue"
                           placeholder="请选择"
                           disabled
                           clearable>
                  <el-option v-for="[key, val] in _dictTransportModeType"
                             :key="key"
                             :label="val"
                             :value="key"></el-option>
                </el-select>
              </el-col>
            </template>
          </el-table-column>
          <el-table-column label="费率"
                           min-width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.rate"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <br />
      <el-row>
        <el-col :span="3">
          <h3>优惠规则设置 </h3>
        </el-col>
      </el-row>
      <hr />
      <br />
      <el-row>
        <el-col>
          <el-button type="info"
                     plain
                     @click="handleDiscountRuleForm">添加优惠规则</el-button>
        </el-col>
      </el-row>
      <el-row class="box"
              v-for="(item,index) in form.preferentials"
              :key="item.id">
        <el-row class="rule">
          <el-col :span="20">计费规则：{{item.feeRuleName}}</el-col>
          <el-col :span="2">
            <el-button @click="selectCondition(item, index)">选择条件</el-button>
          </el-col>
          <el-col :span="2">
            <el-button @click="addFeeRule(item)">添加</el-button>
          </el-col>
        </el-row>
        <el-row class="block">
          <el-col :span="20">
            <span>有效期</span>
            <el-date-picker v-model="item.startDate"
                            :picker-options="startPickerOptions"
                            value-format="yyyy-MM-dd"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
            <span>——</span>
            <el-date-picker v-model="item.endDate"
                            value-format="yyyy-MM-dd"
                            :picker-options="endPickerOptions"
                            type="date"
                            placeholder="选择日期"></el-date-picker>
          </el-col>
          <el-col :span="4">
            <el-button @click="removeFeeRule(item)">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="item.items"
                  border>
          <el-table-column label="序号"
                           min-width="50">
            <template slot-scope="scope">
              {{ scope.$index+1 }}
            </template>
          </el-table-column>
          <el-table-column :key="item2.id"
                           :label="item2.name"
                           v-for="(item2, index2) in item.tableList"
                           min-width="220">
            <template slot-scope="scope">
              <el-row v-if="item.items[scope.$index].factors[index2].name === '货类'">
                <el-col :span="12">
                  <el-select v-model="item.items[scope.$index].factors[index2].operator"
                             placeholder="请选择"
                             clearable>
                    <el-option v-for="[key, val] in _dictRuleOperator"
                               :key="key"
                               :label="val"
                               :value="key"></el-option>
                  </el-select>
                </el-col>
                <el-col :span="12">
                  <el-select v-model="item.items[scope.$index].factors[index2].typeValue"
                             placeholder="请选择"
                             clearable>
                    <el-option v-for="item in selectThingsType"
                               :key="item.cargoTypeCode"
                               :label="item.cargoTypeName"
                               :value="item.cargoTypeCode"></el-option>
                  </el-select>
                </el-col>
              </el-row>
              <el-row v-else-if="item.items[scope.$index].factors[index2].name === '品名'">
                <el-col :span="12">
                  <el-select v-model="item.items[scope.$index].factors[index2].operator"
                             placeholder="请选择"
                             clearable>
                    <el-option v-for="[key, val] in _dictRuleOperator"
                               :key="key"
                               :label="val"
                               :value="key"></el-option>
                  </el-select>
                </el-col>
                <el-col :span="12">
                  <el-select v-model="item.items[scope.$index].factors[index2].typeValue"
                             placeholder="请选择"
                             clearable>
                    <el-option v-for="item in selectGoodsType"
                               :key="item.cargoCode"
                               :label="item.cargoName"
                               :value="item.cargoCode"></el-option>
                  </el-select>
                </el-col>
              </el-row>
              <el-row v-else>
                <el-input v-model="item.items[scope.$index].factors[index2].typeValue"></el-input>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column label="优惠金额"
                           min-width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.rate"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作"
                           align="center"
                           min-width="120">
            <template slot-scope="scope">
              <el-button size="small"
                         type="text"
                         @click="handleAddTable(scope.row,index)">复制</el-button>
              <el-button :disabled="item.items.length===1"
                         type="text"
                         class="danger"
                         size="small"
                         @click="item.items.splice(scope.$index,1)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-col :span="3">
          <h3>入库物资设定 </h3>
        </el-col>
      </el-row>
      <hr />
      <br />
      <div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="品名">
              <auto-cargo searchName="cargoName"
                          v-model="filterText"
                          @select="v=>condition['search_LIKE_cargoName']=v"
                          type="name"></auto-cargo>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-button type="primary"
                       icon="el-icon-search">查 询</el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <!-- 树形菜单 -->
            <el-tree :data="treeData"
                     show-checkbox
                     node-key="id"
                     ref="tree"
                     highlight-current
                     :props="defaultProps"
                     :filter-node-method="filterNode">
            </el-tree>

          </el-col>
        </el-row>
      </div>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button class="ml-80"
                 type="primary"
                 @click="handleSaveContract(false)"
                 :loading="$store.getters.loadingBtn">保存并提交</el-button>
      <el-button @click="dialogForm = false">取 消</el-button>
    </span>
    <add-account-rule-form ref="addAccountRuleForm"
                           @charge="accountRule"></add-account-rule-form>
    <add-account-rule-form2 ref="addAccountRuleForm2"
                            @discount="discountRule"></add-account-rule-form2>
    <select-condition-form ref="selectConditionForm"
                           @condition="getCondition"></select-condition-form>
  </el-dialog>
</template>

<script>
import { getRuleIdRate } from '@/api/balance/balance' //费率
import { addContract } from '@/api/commerce/contract'
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于管辖仓库
import { getCargoTypeList } from '@/api/baseResource/baseResource' //货物种类的下拉菜单
import { getCargoNameList } from '@/api/baseResource/baseResource' //品名的下拉菜单
import { getAccountList } from '@/api/commerce/account'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoCargo from '@/components/autocomplete/autoCargo'
import { PUBLIC_LIMIT, REG_TEXT } from '@/utils/Reg'
import { sortLength } from '@/utils/rules'
import { getLocalTime } from '@/utils/dateFormat'
import attachments from '@/components/file-upload'
import download from '@/components/download'
import addAccountRuleForm from './addAccountRuleForm'
import addAccountRuleForm2 from './addAccountRuleForm2'
import selectConditionForm from './selectConditionForm'

export default {
  name: 'contractForm',
  components: {
    attachments,
    download,
    autoSearchOrg,
    addAccountRuleForm,
    addAccountRuleForm2,
    selectConditionForm,
    autoCargo
  },
  data() {
    return {
      form: this.clearForm(),
      dialogForm: false,
      checked: '',
      checked2: '',
      checked3: '',
      value: '',
      filterText: '',
      selectGoodsType: [], //品名的下拉菜单列表
      treeData: [], //树形结构
      typeData: null,
      condition: {}, // 查询条件
      rules: {
        code: [
          {
            required: true,
            message: '只能输入中文,英文及数字',
            pattern: REG_TEXT,
            trigger: 'change'
          },
          sortLength
        ],
        cargoOwnerAccountName: [
          { required: true, message: '请选择账户名称', trigger: 'change' }
        ],
        startDate: [
          { required: true, message: '请选择有效期起', trigger: 'change' }
        ],
        endDate: [
          { required: true, message: '请选择有效期止', trigger: 'change' }
        ],
        name: [
          {
            required: true,
            validator: PUBLIC_LIMIT,
            message: '请输入合同名称',
            trigger: 'change'
          },
          sortLength
        ],
        signDate: [
          { required: true, message: '请选择签订日期', trigger: 'change' }
        ],
        ifFreeSafeguard: [
          { required: true, message: '请选择是否免保管费', trigger: 'change' }
        ],
        inSettlementMode: [
          { required: true, message: '请选择入库结算方式', trigger: 'change' }
        ],
        outSettlementMode: [
          { required: true, message: '请选择出库结算方式', trigger: 'change' }
        ],
        warehouseName: [
          { required: true, message: '请选择所属仓库', trigger: 'change' }
        ]
      },
      multipleSelection: [], // 选择的行
      checkList: ['非定制费率'],
      // 树形数据
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      accountList: [], //账户名称下拉菜单
      warehouseList: [],
      selectThingsType: [] //货类
    }
  },
  mounted() {
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
    getAccountList().then(res => {
      let data = res.data.aaData
      for (let i = 0; i < data.length; i++) {
        this.accountList.push({ name: data[i].name })
      }
    })
    //货类
    getCargoTypeList().then(res => {
      this.selectThingsType = res.data.aaData
    })
    // 加载树形菜单
    getCargoNameList().then(res => {
      this.selectGoodsType = res.data.aaData
      let data = res.data.aaData
      this.typeData = data
      for (let i = 0; i < data.length; i++) {
        let noData = true
        for (let j = 0; j < this.treeData.length; j++) {
          if (data[i].cargoTypeName === this.treeData[j].label) {
            this.treeData[j].children.push({
              id: data[i].id,
              label: data[i].cargoName,
              code: data[i].cargoCode
            })
            noData = false
            break
          }
        }
        if (noData) {
          this.treeData.push({
            id: data[i].cargoTypeId,
            label: data[i].cargoTypeName,
            code: data[i].cargoTypeCode,
            children: [
              {
                id: data[i].id,
                label: data[i].cargoName,
                code: data[i].cargoCode
              }
            ]
          })
        }
      }
    })
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgName: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name,
        eleCode: null,
        code: '',
        name: '',
        startDate: '',
        endDate: '',
        remark: null,
        signDate: '',
        warehouseCode: '',
        warehouseName: '果园库',
        ifFreeSafeguard: false,
        inSettlementMode: '',
        outSettlementMode: '',
        partyAId: null,
        partyAName: '',
        attachments: null,
        contractRules: [
          {
            id: null,
            feeRuleName: '',
            startDate: '',
            endDate: '',
            rates: [
              {
                sortNo: null,
                id: null,
                rate: null,
                factors: [
                  {
                    name: '',
                    code: '',
                    operator: '',
                    typeValue: '',
                    displayType: ''
                  }
                ]
              }
            ]
          }
        ],
        // feeSettleSettings: [
        //   {
        //     id: 1,
        //     feeRuleName: '',
        //     startDate: '',
        //     endDate: '',
        //     cargoOwnerSettle: false,
        //     settleAccounts: [
        //       {
        //         id: null,
        //         name: ''
        //       }
        //     ]
        //   }
        // ],
        preferentials: [],
        contractCargos: []
      }
    },
    change() {
      console.log(this.value)
    },
    //父组件传值,加载选中的树形菜单
    getData() {
      //加载计费规则表格
      let row = this.form.contractRules
      this.form.contractRules = []
      for (let i = 0; i < row.length; i++) {
        //回填表格
        getRuleIdRate(row[i].id).then(res => {
          let datas = res.data.aaData
          // 生成tableList
          let tmpList = []
          if (datas.length > 0) {
            for (let k = 0; k < datas[0].factor.length; k++) {
              let tmpll = Object.assign({}, datas[0].factor[k])
              tmpList.push(tmpll)
            }
          }
          this.form.contractRules.push({
            id: row[i].id,
            feeRuleName: row[i].feeRuleName,
            rates: datas,
            tableList: tmpList,
            startDate:row[i].startDate,
            endDate:row[i].endDate
          })
        })
      }

      // for (let i = 0; i < this.form.contractRules.length; i++) {
      //   for (let j = 0; j < this.form.contractRules[i].rates.length; j++) {
      //       this.form.contractRules[i].tableList = this.form.contractRules[i].rates[j].factors
      //   }
      // }

      //加载优惠规则表格
      for (let i = 0; i < this.form.preferentials.length; i++) {
        for (let j = 0; j < this.form.preferentials[i].items.length; j++) {
            this.form.preferentials[i].tableList = this.form.preferentials[i].items[j].factors
        }
      }
      this.$nextTick(function() {
        if (this.form.contractCargos) {
          let selectData = []
          this.treeData.forEach(row => {
            for (let i = 0; i < this.form.contractCargos.length; i++) {
              for (let j = 0; j < row.children.length; j++) {
                if (this.form.contractCargos[i].cargoCode === row.children[j].code) {
                  selectData.push({
                    id: row.children[j].id,
                    label: row.children[j].label
                  })
                }
              }
            }
          })
          this.$refs.tree.setCheckedNodes(selectData)
        } else {
          this.$refs.tree.getCheckedNodes([])
        }
      })
    },
    //添加计费规则后的表格展示
    accountRule(row) {
      this.form.contractRules = []
      for (let i = 0; i < row.length; i++) {
        //回填表格
        getRuleIdRate(row[i].id).then(res => {
          let datas = res.data.aaData
          // 生成tableList
          let tmpList = []
          if (datas.length > 0) {
            for (let k = 0; k < datas[0].factor.length; k++) {
              let tmpll = Object.assign({}, datas[0].factor[k])
              tmpList.push(tmpll)
            }
          }
          this.form.contractRules.push({
            id: row[i].id,
            feeRuleName: row[i].feeRuleName,
            rates: datas,
            tableList: tmpList
          })
        })
      }
      this.show2 = true
    },
    //计费规则：点击添加按钮
    addAccountRule(val) {
      this.form.contractRules.push(JSON.parse(JSON.stringify(val)))
    },
    //删除
    removeAccountRule(val) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.form.contractRules.splice(val, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //优惠规则：点击添加按钮
    addFeeRule(val) {
      this.form.preferentials.push(JSON.parse(JSON.stringify(val)))
    },
    //删除
    removeFeeRule(val) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.form.preferentials.splice(val, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    discountRule(row) {
      for (let i = 0; i < row.length; i++) {
        row[i].items = []
        row[i].tableList = []
      }
      this.form.preferentials.push(...row)
      this.show4 = true
    },
    //选择条件之后出现表格
    getCondition(val, index) {
      // 这两行的作用是让vue识别到数据变动
      this.form.preferentials.push({})
      this.form.preferentials.splice(this.form.preferentials.length - 1, 1)

      let factors = []
      this.form.preferentials[index].tableList = []
      for (let i = 0; i < val.length; i++) {
        this.form.preferentials[index].tableList.push({ name: val[i] })
        factors.push({
          name: val[i],
          code: 'cargoTypeCode',
          operator: null,
          typeValue: null
        })
      }
      this.form.preferentials[index].items.push({ rate: '', factors: factors })
    },
    // 树形菜单
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    /*
     * 保存 编辑合同信息
     * */
    handleSaveContract(isApproval) {
      // let contractCargo = this.form.contractCargo
      // if (contractCargo && contractCargo.length === 0) {
      //     this.$message({ message: `至少有一条货物信息`, type: 'error' })
      //     return
      // }
      // for (let i = 0; i < this.form.contractCargo.length; i++) {
      //     // if (!contractCargo[i].cargoTypeName) {
      //     //     this.$message({ message: `请选择货物类型`, type: 'error' })
      //     //     return
      //     // }
      //     if (!contractCargo[i].cargoName) {
      //         this.$message({ message: `请选择货物类型`, type: 'error' })
      //         return
      //     }
      // }
      this.$refs.form.validate(valid => {
        if (valid) {
          addContract(this.form).then(res => {
            this.handleCloseDialog('续签成功!')
          })
        } else {
          this.$message({
            message: '请检查表单',
            type: 'error'
          })
        }
      })
    },
    // 合同提交
    handleApproval(id) {
      contractApprovalPass(id).then(response => {
        this.handleCloseDialog('提交成功!')
      })
    },
    // 新增或编辑成功,关闭弹窗事件
    handleCloseDialog(msg) {
      this.$message({ message: `${msg}`, type: 'success' })
      this.dialogForm = false
      this.$emit('load')
    },
    handleChange(val) {
      getAccountList().then(res => {
        let data = res.data.aaData
        for (let i = 0; i < data.length; i++) {
          if (data[i].name === val) {
            this.form.partyAId = data[i].customerId
            this.form.partyAName = data[i].customerName
          }
        }
      })
    },
    handleCurrentChange() {},
    //添加计费规则
    handleAccountRuleForm() {
      this.$refs.addAccountRuleForm.dialogForm = true
    },
    //添加优惠规则
    handleDiscountRuleForm() {
      this.$refs.addAccountRuleForm2.dialogForm = true
    },
    //条件选择
    selectCondition(row, index) {
      this.$refs.selectConditionForm.getRow(row, index)
      this.$refs.selectConditionForm.dialogForm = true
    },
    //优惠规则复制一行
    handleAddTable(item, index) {
      let params = JSON.parse(JSON.stringify(item))
      params.sortNo = this.form.preferentials[index].items.length + 1
      this.form.preferentials[index].items.push(params)
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    },
    dialogForm(val) {
      if (!val) {
        this.$refs.form.resetFields()
        // this.isEdit = false
        this.form = this.clearForm()
      }
    }
  },
  computed: {
    // 设置有效起始日期
    startPickerOptions() {
      let endDate = this.form.endDate
      return {
        disabledDate(time) {
          // if (time.getTime() < (+new Date()) - 60 * 60 * 24 * 1000) {
          //     return true
          // }
          if (endDate) {
            return time.getTime() > new Date(endDate).getTime()
          } else {
            return false
          }
        }
      }
    },
    // 设置有效截止日期
    endPickerOptions() {
      let startDate = this.form.startDate
      return {
        disabledDate(time) {
          if (startDate) {
            // return time.getTime() < new Date(startDate).getTime() - 60 * 60 * 24 * 1000
            return time.getTime() < new Date(startDate).getTime()
          } else {
            return false
          }
        }
      }
    }
    // 合同类型
    // contractType() {
    //   return this.form.contractType
    // }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.box {
  border: 2px solid #eee;
  margin: 20px 0;
}
.box .title,
.box .rule {
  background: #eee;
  height: 50px;
  line-height: 50px;
  padding-left: 10px;
}
.block {
  margin: 20px 0;
}
</style>
